/**
 * Created by wrl on 2017/7/22.
 */
var path = '../';
$(function(){
    new Vue({
        el: '#form-change-password',
        data: {
            accountInfo:{
                oldPwd:'',
                id:util.getUrlParams()['id'],
                checkAccountPwd:null,
            },
            checkPwdRuleErr:''

        },
        mounted: function () {
            var self = this
            self.$http.get(path+'personInfo/selectById?id='+self.accountInfo.id,{}).then(function (response) {
                if(response.body.sysErrorMsg){
                    tips(response.body.sysErrorMsg,5)
                    return
                }
                self.accountInfo = response.body
            }, function (response) {
                console.log('请求失败')
            })
        },
        methods: {
            update:function(){
                var self = this;
                self.checkPwdRuleErr='';
                if(checkNotEmpty()){
                    if(self.accountInfo.accountPwd != self.accountInfo.checkAccountPwd) return;
                    var regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{6,16}');
                    if (!regex.test(self.accountInfo.accountPwd)){
                        self.checkPwdRuleErr='密码中必须包含字母、数字，至少6个字符，最多16个字符';
                        return;
                    }
                    if(self.accountInfo.accountPwd == self.accountInfo.accountCode){
                        self.checkPwdRuleErr='修改后的密码不能与账户名相同，请重新输入！';
                        return;
                    }
                    self.$http.post(path+'account/changePwd',self.accountInfo).then(function (response) {
                        if(response.body.flag == true){
                            tips('修改成功',6)
                            setTimeout(function(){
                                parent.window.location.replace(parent.window.location.href);
                            },500);
                        }else{
                            if(response.body.sysErrorMsg)
                                tips(response.body.sysErrorMsg,5)
                            else if(response.body.checkErrMsg)
                                tips(response.body.checkErrMsg,5)
                            else
                                tips('修改失败',5)
                        }
                    }, function (response) {
                        console.log('请求失败')
                    })
                }
            }
        }
    })


    $("#form-change-password").Validform({
        tiptype:2,
        callback:function(form){
            form[0].submit();
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    });
})

/**
 * 校验input是否为空
 * @returns {boolean}
 */
function checkNotEmpty(){
    var xx= $('.input-text:not(".noInput")');
    var flag = true;
    $('.input-text:not(".noInput")').each(function(index,ele){
        if($(this).val() == ''){
            $(this).focus().blur();
            flag = false;
            return true;
        }
    });
    return flag;
}